﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AirlineBooking.Models.DTO;

namespace AirlineBooking.Models
{
    public class ReservationModel
    {
        public DTOReservation GetReservationInfo(string PNRNumber)
        {
            using (AirLineDBModelEntities db = new AirLineDBModelEntities())
            {
                Reservation reservation  = db.Reservations.FirstOrDefault(i => i.Code == PNRNumber);
                if (reservation == null)
                {
                    return null;
                }
                DTOReservation result = new DTOReservation() { Code = reservation.Code, ContactName = reservation.ContactName, ContactSurname = reservation.ContactSurname, ContactEmail = reservation.ContactEmail, ContactPhone= reservation.ContactPhone,ContactAddress = reservation.ContactAddress, TotalPrice = reservation.TotalPrice };
                return result;
            }
        }

        public List<DTOIntinerary> GetItinerariesInfo(string PNRNumber)
        {
            using (AirLineDBModelEntities db = new AirLineDBModelEntities())
            {

                Reservation reservation = db.Reservations.FirstOrDefault(i => i.Code == PNRNumber);
                if (reservation == null)
                {
                    return null;
                }
                var result = reservation.Intineraries.Select(i => new DTOIntinerary() { Id = i.Id, Passenger = i.Passenger, Reservation = i.Reservation, Status = i.Status });
                return result.ToList();
            }
        }

        public DTOPassenger GetPassengerInfo(int iD)
        {
            using (AirLineDBModelEntities db = new AirLineDBModelEntities())
            {

                Passenger passenger = db.Passengers.FirstOrDefault(i => i.Id == iD);
                if (passenger == null)
                {
                    return null;
                }
                var result = new DTOPassenger() { Id = passenger.Id, Name = passenger.Name, Surname = passenger.Surname, Title = passenger.Title, Email = passenger.Email, Phone = passenger.Phone, DocumentNumber = passenger.DocumentNumber, DocumentType = passenger.DocumentType ?? 0, Address = passenger.Address, DOB = passenger.DOB ?? DateTime.Now, PassengerType = passenger.PassengerType };
                return result;
            }            
        }
        
        public List<DTOPassenger> GetPassengersInfo(string PNRNumber)
        {
            List<DTOPassenger> result = new List<DTOPassenger>();
            List<DTOIntinerary> listIntinerary = GetItinerariesInfo(PNRNumber);
            foreach (DTOIntinerary intinerary in listIntinerary)
            {
                result.Add(GetPassengerInfo(intinerary.Id));
            }

            return result;
        }

        public DTOFareInfo GetFareInfo(string PNRNumber)
        {
            int numChildren = 0;
            int numAdults = 0;
            int numInfants = 0;
            decimal adultFare = 0;
            decimal infantFare = 0;
            decimal childFare = 0;
            decimal subTotal = 0;
            DTOFareInfo result = new DTOFareInfo();
            List<DTOPassenger> passengerList = new List<DTOPassenger>();
            List<DTOIntinerary> listIntinerary = GetItinerariesInfo(PNRNumber);
            foreach (DTOIntinerary intinerary in listIntinerary)
            {
                passengerList.Add(GetPassengerInfo(intinerary.Id));
            }

            foreach (DTOPassenger passenger in passengerList)
            {
                if (passenger.PassengerType == 1)
                {
                    numAdults++;
                }
                else
                    if (passenger.PassengerType == 2)
                    {
                        numChildren++;
                    }
                    else
                        numInfants++;
            }

            return result;
        }

    }
}